
**#Basic controls

foreach v of global all_DV{
set seed 1234	
use "$Folder/Data_Civil_Servants_final.dta", clear
keep if sample==1

	di in red "`v'"

	qui reg `v' i.treatment i.post i.treat_post $ControlsA, cl(agency)
	loc effect :  di %7.4f _b[1.treat_post]
	
	gen Effect_string=`effect'
	tostring Effect_string, replace
	levelsof Effect_string, local(Effect_string)
	
	cap mat drop bigResults
	forval n=1(1)1000 {
	    
		qui {
		preserve 
			keep agency 
			duplicates drop
			gen random = uniform()
			sort random
			gen size = _N
			gen index = _n
			
			gen treatment_`n'=cond(index <= 50,1,0)
			cap drop random 
			tempfile Treatment
			save `Treatment'
		restore 
		
		merge m:1 agency using `Treatment', nogen
		}
	
		gen treat_post_`n'=treatment_`n'*post
		
		qui reg `v' i.treatment_`n' i.post treat_post_`n' $ControlsA, cl(agency)
		loc bRandom_`n' :  di %7.4f _b[treat_post_`n']
		loc seRandom_`n' : di %7.4f _se[treat_post_`n']

	
		mat resu = [`bRandom_`n'',`seRandom_`n'']
		mat bigResults =nullmat(bigResults) \ resu
			
	}

	drop treatment_* treat_post_*

		mat colname bigResults = beta se
		svmat bigResults, names(col)

		keep if beta!=.
			
keep beta se
rename beta beta_`v' 
rename se se_`v'
save "$Folder/RI_`v'_B1.dta", replace
}

foreach v of global all_DV{
	use "$Folder/RI_`v'_B1.dta", clear
	gen n=_n
	save "$Folder/RI_`v'_B1.dta", replace
}

use "$Folder/RI_business_w5_B1.dta", clear
foreach v of global all_DV{
merge 1:1 n using "RI_`v'_B1.dta", nogen
erase "$Folder/RI_`v'_B1.dta"
}
order n
gen a=1
save "$Folder/RI_B1.dta", replace

use "$Folder/Data_Civil_Servants_final.dta", clear
keep if sample==1

foreach v of global all_DV{
qui reg `v' i.treatment i.post i.treat_post $ControlsA, cl(agency)
gen coef_`v'=_b[1.treat_post]
local t = _b[1.treat_post]/_se[1.treat_post]
gen p_`v'=2*ttail(e(df_r),abs(`t'))
}

gen a=1
collapse a, by(coef* p_*)
merge 1:m a using "$Folder/RI_B1.dta", nogen

mat T = J(3,9,.)

sum coef_business_w5
mat T[1,1] = r(mean)
sum p_business_w5
mat T[2,1] = r(mean)
sum n if abs(beta_business_w5)>=abs(coef_business_w5)
mat T[3,1] = r(N)/1000

matlist T

sum coef_business_w5
mat T[1,1] = r(mean)
sum p_business_w5
mat T[2,1] = r(mean)
sum n if abs(beta_business_w5)>=abs(coef_business_w5)
mat T[3,1] = r(N)/1000

matlist T

sum coef_comply
mat T[1,2] = r(mean)
sum p_comply
mat T[2,2] = r(mean)
sum n if abs(beta_comply)>=abs(coef_comply)
mat T[3,2] = r(N)/1000

sum coef_complete
mat T[1,3] = r(mean)
sum p_complete
mat T[2,3] = r(mean)
sum n if abs(beta_complete)>=abs(coef_complete)
mat T[3,3] = r(N)/1000

sum coef_open_request
mat T[1,4] = r(mean)
sum p_open_request
mat T[2,4] = r(mean)
sum n if abs(beta_open_request)>=abs(coef_open_request)
mat T[3,4] = r(N)/1000

sum coef_extension
mat T[1,5] = r(mean)
sum p_extension
mat T[2,5] = r(mean)
sum n if abs(beta_extension)>=abs(coef_extension)
mat T[3,5] = r(N)/1000

sum coef_business_days_within_15
mat T[1,6] = r(mean)
sum p_business_days_within_15
mat T[2,6] = r(mean)
sum n if abs(beta_business_days_within_15)>=abs(coef_business_days_within_15)
mat T[3,6] = r(N)/1000

sum coef_business_days_within_25
mat T[1,7] = r(mean)
sum p_business_days_within_25
mat T[2,7] = r(mean)
sum n if abs(beta_business_days_within_25)>=abs(coef_business_days_within_25)
mat T[3,7] = r(N)/1000

sum coef_business_days_15
mat T[1,8] = r(mean)
sum p_business_days_15
mat T[2,8] = r(mean)
sum n if abs(beta_business_days_15)>=abs(coef_business_days_15)
mat T[3,8] = r(N)/1000

sum coef_business_days_25
mat T[1,9] = r(mean)
sum p_business_days_25
mat T[2,9] = r(mean)
sum n if abs(beta_business_days_25)>=abs(coef_business_days_25)
mat T[3,9] = r(N)/1000

matlist T

mat rownames T = "Treatment" "p-value"  "p-value RI"
               
frmttable using "$Tables/Table_E7_b1", tex statmat(T) varlabels replace ctitle(" ", business_w5, comply, complete, open_request, extension, business_days_within_15, business_days_within_25, business_days_15, business_days_25) sdec(3)

**#All controls

foreach v of global all_DV{
set seed 1234	
use "$Folder/Data_Civil_Servants_final.dta", clear
keep if sample==1

	di in red "`v'"

	qui reg `v' i.treatment i.post i.treat_post $ControlsB, cl(agency)
	loc effect :  di %7.4f _b[1.treat_post]
	
	gen Effect_string=`effect'
	tostring Effect_string, replace
	levelsof Effect_string, local(Effect_string)
	
	cap mat drop bigResults
	forval n=1(1)1000 {
	    
		qui {
		preserve 
			keep agency 
			duplicates drop
			gen random = uniform()
			sort random
			gen size = _N
			gen index = _n
			
			gen treatment_`n'=cond(index <= 50,1,0)
			cap drop random 
			tempfile Treatment
			save `Treatment'
		restore 
		
		merge m:1 agency using `Treatment', nogen
		}
	
		gen treat_post_`n'=treatment_`n'*post
		
		qui reg `v' i.treatment_`n' i.post treat_post_`n' $ControlsB, cl(agency)
		loc bRandom_`n' :  di %7.4f _b[treat_post_`n']
		loc seRandom_`n' : di %7.4f _se[treat_post_`n']

	
		mat resu = [`bRandom_`n'',`seRandom_`n'']
		mat bigResults =nullmat(bigResults) \ resu
			
	}

	drop treatment_* treat_post_*

		mat colname bigResults = beta se
		svmat bigResults, names(col)

		keep if beta!=.
			
keep beta se
rename beta beta_`v' 
rename se se_`v'
save "$Folder/RI_`v'_B2.dta", replace
}

foreach v of global all_DV{
	use "$Folder/RI_`v'_B2.dta", clear
	gen n=_n
	save "$Folder/RI_`v'_B2.dta", replace
}

use "$Folder/RI_business_w5_B2.dta", clear
foreach v of global all_DV{
merge 1:1 n using "$Folder/RI_`v'_B2.dta", nogen
erase "$Folder/RI_`v'_B2.dta"
}
order n
gen a=1
save "$Folder/RI_B2.dta", replace

use "$Folder/Data_Civil_Servants_final.dta", clear
keep if sample==1

foreach v of global all_DV{
qui reg `v' i.treatment i.post i.treat_post $ControlsB, cl(agency)
gen coef_`v'=_b[1.treat_post]
local t = _b[1.treat_post]/_se[1.treat_post]
gen p_`v'=2*ttail(e(df_r),abs(`t'))
}

gen a=1
collapse a, by(coef* p_*)
merge 1:m a using "$Folder/RI_B2.dta", nogen

mat T = J(3,9,.)

sum coef_business_w5
mat T[1,1] = r(mean)
sum p_business_w5
mat T[2,1] = r(mean)
sum n if abs(beta_business_w5)>=abs(coef_business_w5)
mat T[3,1] = r(N)/1000

matlist T

sum coef_business_w5
mat T[1,1] = r(mean)
sum p_business_w5
mat T[2,1] = r(mean)
sum n if abs(beta_business_w5)>=abs(coef_business_w5)
mat T[3,1] = r(N)/1000

matlist T

sum coef_comply
mat T[1,2] = r(mean)
sum p_comply
mat T[2,2] = r(mean)
sum n if abs(beta_comply)>=abs(coef_comply)
mat T[3,2] = r(N)/1000

sum coef_complete
mat T[1,3] = r(mean)
sum p_complete
mat T[2,3] = r(mean)
sum n if abs(beta_complete)>=abs(coef_complete)
mat T[3,3] = r(N)/1000

sum coef_open_request
mat T[1,4] = r(mean)
sum p_open_request
mat T[2,4] = r(mean)
sum n if abs(beta_open_request)>=abs(coef_open_request)
mat T[3,4] = r(N)/1000

sum coef_extension
mat T[1,5] = r(mean)
sum p_extension
mat T[2,5] = r(mean)
sum n if abs(beta_extension)>=abs(coef_extension)
mat T[3,5] = r(N)/1000

sum coef_business_days_within_15
mat T[1,6] = r(mean)
sum p_business_days_within_15
mat T[2,6] = r(mean)
sum n if abs(beta_business_days_within_15)>=abs(coef_business_days_within_15)
mat T[3,6] = r(N)/1000

sum coef_business_days_within_25
mat T[1,7] = r(mean)
sum p_business_days_within_25
mat T[2,7] = r(mean)
sum n if abs(beta_business_days_within_25)>=abs(coef_business_days_within_25)
mat T[3,7] = r(N)/1000

sum coef_business_days_15
mat T[1,8] = r(mean)
sum p_business_days_15
mat T[2,8] = r(mean)
sum n if abs(beta_business_days_15)>=abs(coef_business_days_15)
mat T[3,8] = r(N)/1000

sum coef_business_days_25
mat T[1,9] = r(mean)
sum p_business_days_25
mat T[2,9] = r(mean)
sum n if abs(beta_business_days_25)>=abs(coef_business_days_25)
mat T[3,9] = r(N)/1000

matlist T

mat rownames T = "Treatment" "p-value"  "p-value RI"
               
frmttable using "$Tables/Table_E7_b2", tex statmat(T) varlabels replace ctitle(" ", business_w5, comply, complete, open_request, extension, business_days_within_15, business_days_within_25, business_days_15, business_days_25) sdec(3)
